<?php

namespace App\Http\Search\Rbac;

use App\Http\Search\BaseSearch;
use App\Http\Model\Rbac\NodeModel;
use HugCode\PhpUnits\Frame\Code\ConstCode;

class NodeSearch extends BaseSearch
{

    /**
     * @Desc 列表
     * @return array
     * @author yashuai<1762910894@qq.com>
     */
    public function index()
    {
        $where = [
            'NodeStatus' => ConstCode::BASIC_STATUS_NORMAL,
        ];
        if(isset($this->params['ShowStatus'])){
            $where['NodeShowType'] = intval($this->params['ShowStatus']);
        }
        $select = [
            'NodeID', 'ControllerName', 'ActionName', 'NodeName', 'NodeOrder', 'NodeShowType',
            'NodeStatus', 'NodeType', 'ParentID', 'NodeIcon'
        ];
        $list   = NodeModel::query()->where($where)
            ->select($select)
            ->orderBy('ParentID')
            ->orderBy('NodeOrder')
            ->get()->toArray();

        $map = [];
        foreach ($list as $item){
            $nodeID = $item['NodeID'];
            $map[$nodeID] = $nodeID;
        }
        $newList = [];
        foreach ($list as $item){
            if($item['ParentID'] == 0 || isset($map[$item['ParentID']])){
                $newList[] = $item;
            }
        }
        return [
            'list'  => $newList,
            'count' => count($newList)
        ];
    }

}
